var express = require('express');
var router = express.Router();
var qiniu =  require('qiniu')

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

router.get('/getToken', function(req, res, next) {
  const accessKey = 'ykab9vqy6Tm7pFk8FSJP1Mf29wOmKFs0h2cP-Xx5';
  const secretKey = 'LtT5Y_VDhyopOUZuAlc-t8M5ng7Fy-9nC2pu122s';
  const mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
  const options = {
    scope: 'chashao-test',
  };
  const putPolicy = new qiniu.rs.PutPolicy(options);
  const uploadToken=putPolicy.uploadToken(mac);
  res.send(uploadToken)
});

router.get('/imgUrl', function(req, res, next) {
  const { key } = req.query
  const accessKey = 'ykab9vqy6Tm7pFk8FSJP1Mf29wOmKFs0h2cP-Xx5';
  const secretKey = 'LtT5Y_VDhyopOUZuAlc-t8M5ng7Fy-9nC2pu122s';
  const mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
  const config = new qiniu.conf.Config();
  const bucketManager = new qiniu.rs.BucketManager(mac, config);
  const privateBucketDomain = 'http://qyfpr5vha.bkt.clouddn.com';
  const deadline = parseInt(Date.now() / 1000) + 3600; // 1小时过期
  const privateDownloadUrl = bucketManager.privateDownloadUrl(privateBucketDomain, key, deadline)
  res.send(privateDownloadUrl)
});

module.exports = router;
